package com.itboye.shop.me.qianbao;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.adapter.SpingAdapter03;
import com.itboye.shop.app.MyApplication;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.BankCodeBean;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.presenter.YuEPresenter;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 添加银行卡2
 */
public class AddBankCardTwoActivity extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;

    EditText add_bank02_phone;
    Button add_bank02_ok;
    String name;
    UserPresenter userPresenter;

    Spinner spinner21;
    String type;//账户类型
    YuEPresenter presenter;
    List<BankCodeBean> typebean;
    SpingAdapter03 adapter;

    LinearLayout kaihuhang;
    EditText yinhangka_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card_two);
        tvTitle.setText("绑定账户");
        userPresenter = new UserPresenter(this);
        presenter = new YuEPresenter(this);
        presenter.onBangDinType();

        String phone = SPUtils.get(MyApplication.instatnce, null, Const.MOBILE, "") + "";
        name = getIntent().getStringExtra("name");
        add_bank02_phone.setText(phone);

        // 当添加银行卡成功之后
        IntentFilter filter1 = new IntentFilter("tianjia.exit");
        registerReceiver(exitBroadcastReceiver, filter1);
    }

    BroadcastReceiver exitBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(exitBroadcastReceiver);
    }

    public void onOnSpinerTwo(final List<BankCodeBean> beans, int i) {

        spinner21.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            // parent： 为控件Spinner view：显示文字的TextView position：下拉选项的位置从0开始
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                type = beans.get(position).getId();
                if (type.equals("6197")) {
                    kaihuhang.setVisibility(View.VISIBLE);
                } else {
                    kaihuhang.setVisibility(View.GONE);
                }
            }

            //没有选中时的处理
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add_bank02_ok:

                if (add_bank02_phone.getText().toString().equals("")) {
                    MAlert.alert("请填写手机号码", Gravity.CENTER);
                } else {
                    if (type.equals("6197")) {
                        if (yinhangka_type.getText().toString().equals("")) {
                            MAlert.alert("请输入开户行", Gravity.CENTER);
                            return;
                        }
                    }
                    showProgressDialog("请稍后...",true);
                    userPresenter.sendVerificationCode("+86", add_bank02_phone.getText().toString(), 2 + "");
                }
                break;

        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == presenter.BangdingType_success) {
                typebean = (List<BankCodeBean>) entity.getData();
                onOnSpinerTwo(typebean, 1);
                adapter = new SpingAdapter03(typebean, AddBankCardTwoActivity.this);
                spinner21.setAdapter(adapter);

            }

            if (entity.getEventType() == UserPresenter.send_code_success) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
                Intent     intent = new Intent(this, AddBankCardThreeActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("type", type);
                intent.putExtra("yhk_type", yinhangka_type.getText().toString());
                intent.putExtra("phone", add_bank02_phone.getText().toString());
                startActivity(intent);
                finish();

            } else if (entity.getEventType() == UserPresenter.send_code_fail) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
        }
    }
}
